package com.ailiwu.www.ailiwu.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ailiwu.www.ailiwu.R;
import com.ailiwu.www.ailiwu.activity.PostRecylerActivity;
import com.ailiwu.www.ailiwu.base.BaseWebViewActivity;
import com.ailiwu.www.ailiwu.bean.ItemBean;
import com.ailiwu.www.ailiwu.url.Urls;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import java.util.ArrayList;

/**
 * Author:    XiYoung
 * Version    V1.0
 * Date:      2015/12/3 000320:20
 * Description:
 * Modification  History:
 * Date         	Author        		Version        	Description
 * -----------------------------------------------------------------------------------
 * 2015/12/3 0003       XiYoung              1.0                   1.0
 * Why & What is modified:
 */
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private ArrayList<ItemBean.DataEntity.ItemsEntity> mDatas;
    private Context mContext;
    private DisplayImageOptions options;
    private ImageLoader imageLoader;

    /***
     * 设置不同的Type
     * Type 1 = "post"
     * Type 2 = "collection"
     */

    private static final String TYPE_POST = "post";
    private static final String TYPE_COLLECTION = "collection";

    private static final int TYPE_POST_VIEW = 0;
    private static final int TYPE_COLLECTION_VIEW = 1;





    public MyRecyclerViewAdapter(Context context, ArrayList<ItemBean.DataEntity.ItemsEntity> mDatas) {
        this.mDatas = mDatas;
        mContext = context;
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
        // 使用DisplayImageOptions.Builder()创建DisplayImageOptions
        options = new DisplayImageOptions.Builder()
                .showImageOnFail(R.drawable.com_taobao_tae_sdk_simple_toast_bg)
                .showImageOnLoading(R.drawable.com_taobao_tae_sdk_simple_toast_bg)
//                .delayBeforeLoading(R.drawable.com_taobao_tae_sdk_simple_toast_bg)
                .cacheInMemory(true)                        // 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)                          // 设置下载的图片是否缓存在SD卡中
                .displayer(new RoundedBitmapDisplayer(20))   // 设置成圆角图片
                .build();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == TYPE_COLLECTION_VIEW ) {
            View view = View.inflate(mContext, R.layout.item_recyclerview_home_first_collection, null);
            return new CollectionViewHolder(view);
        } else {
            View view = View.inflate(mContext, R.layout.item_recyclerview_home_first, null);
            return new PostViewHolder(view);
        }
    }


    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        ItemBean.DataEntity.ItemsEntity itemsEntity = mDatas.get(position);
        String type = itemsEntity.getType();
        if (holder instanceof PostViewHolder) {
            final String title = itemsEntity.getTitle();
            final String url = itemsEntity.getCover_image_url();
            int published_at = itemsEntity.getPublished_at();
            int nextUpdataTime = itemsEntity.getUpdated_at() - published_at;
            final int likedNum = itemsEntity.getLikes_count();
            boolean isLiked = itemsEntity.isLiked();
            final String content_url = itemsEntity.getContent_url();

            ((PostViewHolder) holder).tvTitle.setText(title);
            ((PostViewHolder) holder).tvLikeNum.setText(likedNum + "");
            ((PostViewHolder) holder).tvNextUpdataTime.setText(nextUpdataTime + "");
            ((PostViewHolder) holder).tvUpdataTime.setText(published_at + "");
            ((PostViewHolder) holder).cbLiked.setChecked(isLiked);
            ((PostViewHolder) holder).cbLiked.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                  int  num = likedNum;
                    if(isChecked){
                        ((PostViewHolder) holder).tvLikeNum.setText(String.valueOf(++num));
                    }else{
                        ((PostViewHolder) holder).tvLikeNum.setText(String .valueOf(num));
                    }
                }
            });
            ((PostViewHolder) holder).view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, BaseWebViewActivity.class);
                    intent.putExtra("iamge_url",url);
                    intent.putExtra("content_url",content_url);
                    intent.putExtra("title",title);
                    mContext.startActivity(intent);
                }
            });

            imageLoader.displayImage(url, ((PostViewHolder) holder).ivBg, options);
        } else if (holder instanceof CollectionViewHolder) {
            final String title = itemsEntity.getTitle();
            String url = itemsEntity.getCover_image_url();
            String subtitle = itemsEntity.getSubtitle();

            final int id = itemsEntity.getId();
            //设置布局
            imageLoader.displayImage(url, ((CollectionViewHolder) holder).ivBg, options);
            ((CollectionViewHolder) holder).tvTitle.setText(title);
            ((CollectionViewHolder) holder).tvSubTitle.setText(subtitle);
            ((CollectionViewHolder) holder).view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String urlID = Urls.PostsID(id);
                    Intent intent = new Intent(mContext, PostRecylerActivity.class);
                    intent.putExtra("urlID", urlID);
                    intent.putExtra("title", title);
                    mContext.startActivity(intent);
                }
            });
        }


    }

    @Override
    public int getItemCount() {

        return mDatas == null ? 0 : mDatas.size();
    }

    @Override
    public int getItemViewType(int position) {
        ItemBean.DataEntity.ItemsEntity itemsEntity = mDatas.get(position);
        String type = itemsEntity.getType();
        if (TYPE_POST.equals(type)) {
            return TYPE_POST_VIEW;
        } else if (TYPE_COLLECTION.equals(type)) {
            return TYPE_COLLECTION_VIEW;
        }
        return super.getItemViewType(position);
    }

    public static class PostViewHolder extends RecyclerView.ViewHolder {
        TextView tvUpdataTime, tvNextUpdataTime, tvTitle, tvLikeNum;
        ImageView ivBg;
        CheckBox cbLiked;
        View view;
        LinearLayout llTop;

        public PostViewHolder(final View itemView) {
            super(itemView);
            view = itemView;
            tvUpdataTime = (TextView) itemView.findViewById(R.id.tv_time_updata);
            tvNextUpdataTime = (TextView) itemView.findViewById(R.id.tv_time_next_updata);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            tvLikeNum = (TextView) itemView.findViewById(R.id.tv_liked_num);
            ivBg = (ImageView) itemView.findViewById(R.id.iv_image);
            cbLiked = (CheckBox) itemView.findViewById(R.id.checkBox_liked);
            llTop = (LinearLayout) itemView.findViewById(R.id.item_header);

        }
    }

    public static class CollectionViewHolder extends RecyclerView.ViewHolder {
        TextView tvTitle, tvSubTitle;
        ImageView ivBg;
        View view;
        LinearLayout llTop;

        public CollectionViewHolder(View itemView) {
            super(itemView);
            view = itemView;
            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            tvSubTitle = (TextView) itemView.findViewById(R.id.tv_subtitle);
            ivBg = (ImageView) itemView.findViewById(R.id.iv_image);
            llTop = (LinearLayout) itemView.findViewById(R.id.item_header);
        }
    }

}
